<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Banner extends CI_Controller
{
    public $_module_name = "";
	function __construct()
	{
        parent::__construct();
        $this->_module_name = $this->router->fetch_module();
        $this->lang->load('banner');
		$this->load->model('banner_model','banner');
		$this->load->config('config_position') ;
	  	$this->session->set_userdata(array('Url'=>uri_string()));   
		
		$this->pre_message = '' ;
	}
	
	function index($start=0)
	{
		$data['title'] = 'Quản lý banner';
		$data['add'] = $this->_module_name.'/add';
		
		$config['total_rows']   =  $this->banner->get_num_banner();
		$data['num'] = $config['total_rows'];
		$per_page = $this->config->item('per_page');
		$config['per_page']  =  20;
		$config['uri_segment'] = 3; 
		$this->pagination->initialize($config);
		$data['pagination']    = $this->pagination->create_links();
		
        $data['list'] =   $this->banner->getAllBanner($config['per_page'],$start); //trit($data['list']) ;
		$data['start'] = $start ;
        $data['page'] = 'banner/index' ;
		$this->load->view('layout/skin', $data);
	}
	/**
	Date 	: 05/05/2014
	Desc 	: add new record
	Params 	: option
	Return 	: showview
	**/
	function add()
	{
		$data['title'] = 'Thêm mới quảng cáo';
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_url',lang('banner.link'),'trim|required');
			$this->form_validation->set_rules('lb_image',lang('image'),'trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$req =  $this-> build_data(1);
				if($this->banner->saveBanner(0,$req)){
					$this->session->set_flashdata('message',lang('successful'));
					redirect($this->_module_name.'/banner');
				}
			}
		}
		
		$data['message'] = $this->pre_message;
		$data['page'] = 'banner/add' ;
		$this->load->view('layout/skin', $data);
	}  

	function edit($banner_id=0)
	{
        $data['title'] = lang('update');
		
        $data['rs'] = $this->banner->getBannerByID($banner_id);
        if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_url',lang('banner.link'),'trim|required');
			//$this->form_validation->set_rules('lb_image',lang('image'),'trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$req =  $this->build_data();
				if($req['lb_image']) $this->delimg($data['rs']->lb_image);
				if($this->banner->saveBanner($banner_id,$req))
				{
					$this->session->set_flashdata('message',lang('successful'));
					redirect($this->_module_name.'/banner');
				}
			}
		}
		$data['message'] = $this->pre_message;
		$data['page'] = 'banner/edit' ;
		$this->load->view('layout/skin', $data);
	}  
	
	private function build_data($flag=0)
	{
		$req["lb_title"]	= $this->input->post("lb_title");
		$req["lb_url"]	= $this->input->post("lb_url");
		$req["kind"]	= (int)$this->input->post("kind");
		$req["nb_order"]	= (int)$this->input->post("nb_order");
		
		$req["dt_modify"] 	= date('Y-m-d H:i:s');
		if($flag) $req["dt_create"] 	= date('Y-m-d H:i:s');
		$req["bl_active"] 	= (int)$this->input->post("bl_active");
		//upload hinh anh
		$img = $this->input->post("lb_image");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/banner/'.$img_name))
			{
				@unlink('../uploads/banner/'.$img_name);
			}
			//fn_resize_image('../uploads/'.$img,'../uploads/banner/'.$img_name, 300, 250);
			@copy('../uploads/'.$img,'../uploads/banner/'.$img_name);
			@unlink('../uploads/temp/'.$img_name); 
			$req["lb_image"] = $img_name;
		}
		return $req;
	}
	
	private function delimg($filename='')
	{
		
		if(!empty($filename) && file_exists('../uploads/category/'.$filename))
		{
			@unlink('../uploads/category/'.$filename);
		}
	}
	
	function del($banner_id,$offset=0)
	{
		if($this->banner->delBanner($banner_id))
			$this->session->set_flashdata('message',lang('delete_success'));
		else $this->session->set_flashdata('message',lang('delete_unsuccess'));
		// xóa các tin đã bo sau 3 tháng
		$this->banner->remove() ;
		redirect($this->_module_name.'/banner/index/'.$offset);
	}
	/**
	Date 	: 05/05/2014
	Desc 	: show list
	Params 	: option
	Return 	: showview
	**/
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$offset = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
					
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->banner->delBanner($ar_id[$i]))
							$this->session->set_flashdata('message',lang('delete_success'));
						else $this->session->set_flashdata('message',lang('delete_unsuccess'));
					}
				}
			}
		}
		redirect($this->_module_name.'/banner/index/'.$offset);
	}  
	/**
	Date 	: 05/05/2014
	Desc 	: show list
	Params 	: option
	Return 	: showview
	**/
	function top()
	{
		$data['title'] = 'Quản lý banner top';
		$data['bntop_name'] = $this->config->item('bntop_name');
		$data['bntop_link'] = $this->config->item('bntop_link');
		$data['bntop_img'] = $this->config->item('bntop_img');
		/*$data['bntop_width'] = $this->config->item('bntop_width');
		$data['bntop_height'] = $this->config->item('bntop_height');*/
		$data['bntop_active'] = $this->config->item('bntop_active');
		
		$this->form_validation->set_rules('bntop_name','Tên quảng cáo','required');
		$this->form_validation->set_rules('bntop_link','Link quảng cáo','required');
		$this->form_validation->set_rules('bntop_img','Hình ảnh quảng cáo','required');
		//$this->form_validation->set_rules('bntop_width','Chiều rộng quảng cáo','required|numeric');
		//$this->form_validation->set_rules('bntop_height','Chiều cao quảng cáo','required|numeric');
		
		if($this->form_validation->run() == false)
		{
		   $this->pre_message = validation_errors(); 
		}
		else
		{
		  $bntop_name = $this->input->post('bntop_name');
		  $bntop_link = $this->input->post('bntop_link');
		  $bntop_img = $this->input->post('bntop_img');
		  /*$bntop_width = $this->input->post('bntop_width');
		  $bntop_height = $this->input->post('bntop_height');*/
		  $bntop_active = (int)$this->input->post('bntop_active');             
		  
		  $str = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
		  $str .= "\n";
		  $str .= "\n\$config['bntop_name'] = '$bntop_name';"; ;
		  $str .= "\n\$config['bntop_link'] = '$bntop_link';"; ;
		  $str .= "\n\$config['bntop_img'] = '$bntop_img';"; ;
		  //$str .= "\n\$config['bntop_width'] = $bntop_width;"; ;
		  //$str .= "\n\$config['bntop_height'] = $bntop_height;"; ;
		  $str .= "\n\$config['bntop_active'] = $bntop_active;"; ;
		  $str .= "\n";                      
		  $str .= "\n\n/* End of file*/\n?>";        
		  write_file('../config/banner_top.php', $str);  
		   
		  $data['bntop_name'] = $this->config->item('bntop_name');
		  $data['bntop_link'] = $this->config->item('bntop_link');
		  $data['bntop_img'] = $this->config->item('bntop_img');
		  //$bntop_width = $this->input->post('bntop_width');
		  //$bntop_height = $this->input->post('bntop_height');              
		  $data['bntop_active'] = $this->config->item('bntop_active');    
		  $this->session->set_flashdata('message',"Lưu thành công");               
		  redirect($this->_module_name.'/banner/top');
		}          
		$this->_templates['page'] = 'banner/banner_top';
		$this->site_library->load($this->_templates['page'],$data);
	}
	/**
	Date 	: 05/05/2014
	Desc 	: show list
	Params 	: option
	Return 	: showview
	**/
	function artist()
	{
		$data['title'] = 'Cập nhật artist banner';
		$data['artist_name'] = $this->config->item('artist_name');
		$data['artist_link'] = $this->config->item('artist_link');
		$data['artist_img'] = $this->config->item('artist_img');
		/*$data['artist_width'] = $this->config->item('artist_width');
		$data['artist_height'] = $this->config->item('artist_height');*/
		$data['artist_active'] = $this->config->item('artist_active'); 
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('artist_name','Tên quảng cáo','required');
			$this->form_validation->set_rules('artist_link','Link quảng cáo','required');
			$this->form_validation->set_rules('artist_img','Hình ảnh quảng cáo','required');
			//$this->form_validation->set_rules('artist_width','Chiều rộng quảng cáo','required|numeric');
			//$this->form_validation->set_rules('artist_height','Chiều cao quảng cáo','required|numeric');
			
			if($this->form_validation->run() == false)
			{
			   $this->pre_message = validation_errors(); 
			}
			else
			{
				$artist_name = $this->input->post('artist_name');
				$artist_link = $this->input->post('artist_link');
				$artist_img = $this->input->post('artist_img');
				/*$artist_width = $this->input->post('artist_width');
				$artist_height = $this->input->post('artist_height');*/
				$artist_active = (int)$this->input->post('artist_active');             
				
				  // write file
				$str = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
				$str .= "\n";
				$str .= "\n\$config['artist_name'] = '$artist_name';"; ;
				$str .= "\n\$config['artist_link'] = '$artist_link';"; ;
				$str .= "\n\$config['artist_img'] = '$artist_img';"; ;
				//$str .= "\n\$config['artist_width'] = $artist_width;"; ;
				//$str .= "\n\$config['artist_height'] = $artist_height;"; ;
				$str .= "\n\$config['artist_active'] = $artist_active;"; ;
				$str .= "\n";                      
				$str .= "\n\n/* End of file*/\n?>";        
				write_file('../config/banner_artist.php', $str); 
				
				$data['artist_name'] = $this->config->item('artist_name');
				$data['artist_link'] = $this->config->item('artist_link');
				$data['artist_img'] = $this->config->item('artist_img');
				//$artist_width = $this->input->post('artist_width');
				//$artist_height = $this->input->post('artist_height');              
				$data['artist_active'] = $this->config->item('artist_active');    
				$this->session->set_flashdata('message',"Lưu thành công");               
				redirect($this->_module_name.'/banner/artist');
			}
		}
		$this->_templates['page'] = 'banner/banner_artist';
		$this->site_library->load($this->_templates['page'],$data);
	}

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */